<?php

/**
 * @Author: kendy
 * @Date:   2020-03-17 10:18:20
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-03-27 13:34:43
 */

namespace app\common\model; 
use think\Model;
use think\Db; 
use think\loader;
class OrderRefund extends Model
{
	public function weixinRefund($obj){
		  //微信退款 
            Loader::import('WxPay.WxPay',EXTEND_PATH,'.Api.php');
            $config = new \WxPayConfig();
            $WxPay  = new \WxPayApi();
            $WxData = new \WxPayRefund();
            $appId = $config->GetAppId();
            $mchId = $config->GetMerchantId();
            $nonceStr = $WxPay->getNonceStr();
            $out_refund_no = makeOrderSn();
            $refund_desc = $obj['refund_reason']; 
            $total_fee = $obj['order_price'] *100;
            $refund_fee = $obj['order_price']*100;
            $out_trade_no = $obj['order_sn']; 
            $WxData->SetAppid($appId);
            $WxData->SetMch_id($mchId);
            $WxData->SetNonce_str($nonceStr);
            $WxData->SetTotal_fee($total_fee);
            $WxData->SetOut_trade_no($out_trade_no);
            $WxData->SetOp_user_id($mchId);
            $WxData->SetOut_refund_no($out_refund_no);
            $WxData->SetRefund_fee($refund_fee); 
            $WxData->SetSign($config);
            $WxData->SetNotify_url("http://api.lancly.com/api/Wxpays/WexinNotify");
            $return = $WxPay->refund($config,$WxData); 
            if ($return['return_code'] == 'SUCCESS'){
                return 'SUCCESS'; 
            }else{    
                return 'ERROR';
            }

	}


   /**
    * [aliRefund 支付宝退款]
    * @Author   kendy
    * @DateTime 2020-03-17T10:32:22+0800
    * @param    [type]                   $obj [description]
    * @return   [type]                        [description]
    */
	public function aliRefund($obj){

            Loader::import('Alipay.aop.AopClient');
            Loader::import('Alipay.aop.request.AlipayTradeRefundRequest');
            $aop = new \AopClient ();
            $aop->gatewayUrl = config('alipay')['gatewayUrl'];
            $aop->appId = config('alipay')['appId'];
            $aop->rsaPrivateKey =config('alipay')['rsaPrivateKey'];
            $aop->alipayrsaPublicKey=config('alipay')['alipayrsaPublicKey'];
            $aop->apiVersion = '1.0';
            $aop->signType =config('alipay')['signType'];
            $aop->postCharset='UTF-8';
            $aop->format=config('alipay')['format'];


            $request = new \AlipayTradeRefundRequest(); 
            $bizcontent = json_encode([ 'out_trade_no'=>$obj['order_sn'],'refund_amount'=> $obj['order_price'], 'refund_reason'=>$obj['refund_reason'] ]);
            $request->setBizContent($bizcontent);
            $result = $aop->execute($request);
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
             if($resultCode==10000){ 
                //新增订单日志
                $logOrder = [];
                $logOrder['order_id'] = $obj['order_id'];
                $logOrder['order_status'] = 4;
                $logOrder['log_content'] = "订单退款";
                $logOrder['user_id'] = $obj['uid'];
                $logOrder['create_time'] = time();
                $logOrder['order_type'] = 4;
                Db::name('log_orders')->insert($logOrder);
                //创建一条支付流水记录
                $lm = [];
                $lm['target_type'] = 0;
                $lm['target_id'] = $obj['uid'];
                $lm['data_id'] = 1;
                $lm['data_src'] = 1;
                $lm['remark'] = '订单【'.$obj['order_sn'].'】退款￥'.$obj['order_price'];
                $lm['money_type'] = 0;
                $lm['money'] =$obj['order_price'];
                $lm['pay_type'] = $obj['pay_type'];
                $lm['create_time'] = date('Y-m-d H:i:s');
                Db::name('log_moneys')->insert($lm); 
               return 'SUCCESS';
            }else{
              return 'ERROR';
            }

	}
	 
}